<template>
  <div class="login-page">
    <el-card class="el-card">
      <template #header>黑马面经运营后台</template>
      <el-form ref="form" :model="form" :rules="rules" autocomplete="off">
        <el-form-item label="用户名：" prop="username">
          <el-input v-model="form.username" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="密码：" prop="password">
          <el-input v-model="form.password" placeholder="请输入密码：" />
        </el-form-item>
        <el-form-item>
          <el-button @click="login" type="primary">登录</el-button>
          <el-button @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      form: {
        username: "admin",
        password: "admin",
      },
      rules: {
        username: [
          {
            required: true,
            message: "请输入用户名",
            trigger: ["blur", "change"],
          },
          {
            min: 5,
            max: 11,
            message: "长度在 5 到 11 个字符",
            trigger: ["blur", "change"],
          },
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: ["blur", "change"],
          },
          {
            pattern: /^\w{5,}$/,
            message: "请输入5位数以上的密码",
            trigger: ["blur", "change"],
          },
        ],
      },
    };
  },
  methods: {
    async login () {
      try {
        const valid = await this.$refs.form.validate()
        // console.log(valid)
        await this.$store.dispatch('user/asyncLogin', this.form)
        this.$router.push('/')
        this.$message.success('登录成功')
      } catch (e) {
        console.log(e)

      }
    },
    reset () {
      this.$refs.form.resetFields()
    }
  }
};
</script>

<style lang="scss" scoped>
.login-page {
  min-height: 100vh;
  background: url(@/assets/login-bg.svg) no-repeat center / cover;
  display: flex;
  align-items: center;
  justify-content: center;
  .el-card {
    width: 420px;
    ::v-deep .el-card__header {
      height: 80px;
      line-height: 40px;
      background: rgba(114, 124, 245, 1);
      text-align: center;
      color: #fff;
      font-size: 18px;
    }
  }
  .el-form {
    padding: 0 20px;
  }
  .tc {
    text-align: center;
  }
}
</style>
